# == Schema Information
# Schema version: 36
#
# Table name: volumes
#
#  id            :integer       not null, primary key
#  volume_number :integer       not null
#  description   :string(255)   
#  created_at    :datetime      not null
#  updated_at    :datetime      not null
#

# A Volume is a collection of Issues, this is
# a way to organize the diferent issues and is mostly
# a syntactic category.
class Volume < ActiveRecord::Base

    has_many :issues

    validates_presence_of :volume_number
    validates_uniqueness_of :volume_number
    validates_numericality_of :volume_number

#   To get the last volume created
    def self.get_last_volume
        
    end
#     Escaping html
    def before_save
      ERB::Util.h(self.volume_number)
    end

    def self.find_non_empty
      self.find_by_sql('SELECT volume_id  FROM  issues GROUP BY volume_id')
    end
    
    #Prevent deletion unless there is not sections
    def before_destroy
      if self.issues.count >0
        raise "This volume already has issues associated delete them first"
      end
    end

end
